package com.ac.api.group.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.annotation.SysLog;
import com.base.helper.BaseController;
import com.base.helper.Result;
import com.ac.api.group.entity.AcActionPage;
import com.ac.api.group.service.AcActionPageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mzl
 * @since 2021-01-25
 */
@Api(tags = "功能页面")
@RestController
@RequestMapping("/acActionPage")
public class AcActionPageController extends BaseController {

    private static final Logger              LOG = LoggerFactory.getLogger(AcActionPageController.class);
    @Autowired
    private              AcActionPageService entityService;

    @SysLog(action = "findByPage", value = "分页查询功能页面")
    @ApiOperation(value = "分页查询功能页面", notes = "分页查询功能页面")
    @RequestMapping(method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "size", value = "分页大小", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面：从1开始", paramType = "query")})
    public Result<IPage<AcActionPage>> findByPage(final AcActionPage example, final Page page) {
        IPage<AcActionPage> records = entityService.page(example, page);
        return new Result(true, records);
    }

    @SysLog(action = "delete", value = "删除功能页面")
    @ApiOperation(value = "删除功能页面")
    @RequestMapping(value = "{id}", method = {RequestMethod.DELETE})
    @ApiImplicitParam(name = "id", value = "功能页面ID", required = true, paramType = "path")
    public Result delete(@PathVariable final Integer id) {
        entityService.removeById(id);
        return new Result(true, "成功删除功能页面", null);
    }

    @SysLog(action = "one", value = "查询单个功能页面")
    @ApiOperation(value = "查询单个功能页面")
    @RequestMapping(value = "{id}", method = {RequestMethod.GET})
    @ApiImplicitParam(name = "id", value = "功能页面ID", required = true, paramType = "path")
    public Result one(@PathVariable final Integer id) {
        AcActionPage entity = entityService.getById(id);
        return new Result(true, entity);
    }

    @SysLog(action = "add", value = "添加功能页面")
    @ApiOperation(value = "添加功能页面")
    @RequestMapping(method = {RequestMethod.POST})
    public Result add(@Valid @RequestBody final AcActionPage entity) {
        entityService.save(entity);
        return new Result(true, "成功保存功能页面", null);
    }

    @SysLog(action = "update", value = "修改功能页面")
    @ApiOperation(value = "修改功能页面")
    @RequestMapping(method = {RequestMethod.PUT})
    public Result update(@Valid @RequestBody final AcActionPage entity) {
        if (null == entity.getId()) {
            throw new RuntimeException("id不能为空");
        }
        entityService.updateById(entity);
        return new Result(true, "成功修改功能页面", null);
    }

    @ApiOperation(value = "全部功能页面")
    @RequestMapping(value = "all", method = RequestMethod.GET)
    public Result<List<AcActionPage>> all(AcActionPage example) {
        List<AcActionPage> entitys = entityService.list(example);
        if (null != entitys) {
            return new Result<>(true, entitys);
        }
        return new Result<>(true, Collections.emptyList());
    }

}

